﻿using System;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.Google;
using Owin;
using LostAndFound.Web.Models;
using LostAndFound.Data.Entities;

namespace LostAndFound.Web
{
    public partial class Startup
    {
        // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
        public void ConfigureAuth(IAppBuilder app)
        {
            // Configure the db context, user manager and signin manager to use a single instance per request
            app.CreatePerOwinContext(AppDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

            // Enable the application to use a cookie to store information for the signed in user
            // and to use a cookie to temporarily store information about a user logging in with a third party login provider
            // Configure the sign in cookie

            //TODO: Cigdem revisit this section for changed password
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                CookieName = "LostAndFoundWebApp",
                CookiePath = "/",
                ExpireTimeSpan = TimeSpan.FromHours(1),
                SlidingExpiration = true,
                Provider = new CookieAuthenticationProvider
                {
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, AppUser, Guid>(
                    validateInterval: TimeSpan.FromMinutes(120),
                   regenerateIdentityCallback:(manager,user)=>(user.GenerateUserIdentityAsync(manager,DefaultAuthenticationTypes.ApplicationCookie)),
                   getUserIdCallback: (user) => Guid.Parse(user.GetUserId()))

                },
                AuthenticationMode =Microsoft.Owin.Security.AuthenticationMode.Active,
                

            });

            

        }
    }
}